module.exports = class extends think.Controller {
  async __before() {
    //  是否登录
    const adminInfo = await this.getUser();
    if (adminInfo) {
      //  有登录
      this.adminInfo = adminInfo;
    } else {
      //  未登录
      if (this.ctx.isAjax()) {
        return this.json({code: 401, data: null, msg: '未登录或登录已过期'});
      } else {
        this.redirect('/login/');
      }
    }
  }
  /**
   * 取缓存登录用户信息
   */
  async getUser() {
    const token = this.cookie('admin') || this.header('token') || '';
    //
    if (think.isEmpty(token)) {
      return null;
    } else {
      let userStr = await think.cache(token);
      userStr = JSON.parse(userStr);
      if (think.isEmpty(userStr)) {
        return null;
      } else {
        return userStr;
      }
    }
  }
};
